/**
 * @author 老刘
 * @desc 日期选择
 */
import {showLoading, hideLoading, formatNumber, getModifyTime} from '../../../utils/util';
const app = getApp();
Page({
	data: {
		// 可以选当前月,不能选当日
		calendarData: {
			startDateYear: undefined,
			startDateMonth: undefined,
			today: undefined,
			year: undefined,
			month: undefined,
			selectDate: undefined,
			currentMonth: undefined,
			selectMonth: undefined,
			date: ['日', '一', '二', '三', '四', '五', '六'],
			datePickerTab: [
				{name: '日订单'},
				{name: '月订单'}
			],
			currentTab: 0,
			dateArr: [],
			calendarList: [],// 日历数组
			monthList: [],// 月份数组
			toView: undefined
		}
	},
	onLoad () {
		showLoading();
		this.mySetData({
			startDateYear: app.globalData.startDateYear,
			startDateMonth: app.globalData.startDateMonth
		});
		if (app.globalData.calendarData) {
			this.setData({
				calendarData: app.globalData.calendarData
			});
		} else {
			this.init();
		}
		if (app.globalData.choiceDateData.type === 1) {
			this.mySetData({
				selectDate: app.globalData.choiceDateData.choiceData,
				toView: app.globalData.choiceDateData.choiceData.substring(0, 6),
				currentTab: 0
			});
		} else {
			this.mySetData({
				selectMonth: app.globalData.choiceDateData.choiceData,
				toView: app.globalData.choiceDateData.choiceData.substring(0, 4),
				currentTab: 1
			});
		}
		hideLoading();
		app.globalData.calendarData = this.data.calendarData;
	},
	// 初始化所有数据
	init () {
		// 可以选当前月,不能选当日
		let now = new Date();
		// let year = 2021;
		let year = now.getFullYear();
		let month = now.getMonth() + 1;
		this.mySetData({
			year: year,
			month: month,
			today: `${year}${formatNumber(month)}${formatNumber(now.getDate())}`,
			toView: `${year}${formatNumber(month)}${formatNumber(now.getDate())}`.substring(0, 6),
			selectDate: getModifyTime(`${year}${formatNumber(month)}${formatNumber(now.getDate())}`, -1),
			currentMonth: `${year}${formatNumber(month)}`,
			selectMonth: `${year}${formatNumber(month)}`
		});
		let differYear = year - this.data.calendarData.startDateYear;
		// 计算当前月到起始月日历数据
		for (let i = 0; i <= differYear; i = i + 1) {
			let mouthList = [];
			let obj;
			if (i === 0) {
				// 当前年,只需计算当前月之前的
				for (let k = 0; k < month; k = k + 1) {
					obj = {
						year,
						month: month - k,
						currentDate: year + formatNumber(month - k)
					};
					mouthList.push(obj);
					this.dateInit(year, month - k - 1);
				}
			} else if (i === differYear) {
				// 起始年份 计算起始月~12月的
				for (let k = 0; k <= 12 - this.data.calendarData.startDateMonth; k = k + 1) {
					obj = {
						year: this.data.calendarData.startDateYear,
						month: parseInt(this.data.calendarData.startDateMonth) + k,
						currentDate: `${this.data.calendarData.startDateYear}${formatNumber(parseInt(this.data.calendarData.startDateMonth) + k)}`
					};
					mouthList.push(obj);
					this.dateInit(this.data.calendarData.startDateYear, 12 - k - 1);
				}
			} else {
				// 计算1到12月份的
				for (let k = 0; k < 12; k = k + 1) {
					obj = {
						year: year - i,
						month: 12 - k,
						currentDate: year - i + formatNumber(12 - k)
					};
					mouthList.push(obj);
					this.dateInit(year - i, 12 - k - 1);
				}
			}
			let currentObj = {
				year: year - i,
				mouthList: mouthList.reverse()
			};
			this.mySetData({
				[`calendarData.mouthList[${differYear - i}]`]: currentObj
			});
		}
		this.mySetData({
			mouthList: this.data.calendarData.mouthList.reverse()
		});
	},
	// 属性值不能为undefined
	mySetData (obj) {
		let calendarDataList = [
			'startDateYear',
			'startDateMonth',
			'today',
			'year',
			'month',
			'selectDate',
			'currentMonth',
			'selectMonth',
			'date',
			'datePickerTab',
			'currentTab',
			'dateArr',
			'calendarList',
			'monthList',
			'toView'
		];
		let tergetKeys = Object.keys(obj);
		let calendarData = this.data.calendarData;
		let tempObj = {};
		for (let key of tergetKeys) {
			if (calendarDataList.indexOf(key) !== -1) {
				calendarData[key] = obj[key];
			} else {
				tempObj[key] = obj[key];
			}
		}
		tempObj['calendarData'] = calendarData;
		this.setData(tempObj);
	},
	// 日期初始化
	dateInit (setYear, setMonth) {
		// 全部时间的月份都是按0~11基准，显示月份才+1
		let dateArr = []; // 需要遍历的日历数组数据
		let arrLen = 0; // dateArr的数组长度
		let now = setYear ? new Date(setYear, setMonth) : new Date();
		let year = setYear || now.getFullYear();
		let nextYear = 0;
		let month = setMonth || now.getMonth(); // 没有+1方便后面计算当月总天数
		let nextMonth = (month + 1) > 11 ? 1 : (month + 1);
		let startWeek = new Date(year + '/' + (month + 1) + '/' + 1).getDay(); // 目标月1号对应的星期
		let dayNums = new Date(year, nextMonth, 0).getDate(); // 获取目标月有多少天
		let obj = {};
		let num = 0;
		if (month + 1 > 11) {
			nextYear = year + 1;
			dayNums = new Date(nextYear, nextMonth, 0).getDate();
		}
		arrLen = startWeek + dayNums;
		for (let i = 0; i < arrLen; i++) {
			if (i >= startWeek) {
				num = i - startWeek + 1;
				obj = {
					isToday: `${year}${formatNumber(month + 1)}${formatNumber(num)}`,
					dateNum: num
				};
			} else {
				obj = {};
			}
			dateArr[i] = obj;
		}
		let calendar = {
			currentYear: year,
			currentMouth: month + 1,
			currentDate: year + formatNumber(month + 1),
			dateArr
		};
		this.data.calendarData.calendarList.push(calendar);
		this.mySetData({
			dateArr: dateArr,
			calendarList: this.data.calendarData.calendarList
		});
	},
	onClickSelectDate (e) {
		let item = e.currentTarget.dataset['item'];
		if (JSON.stringify(item) === '{}') {
			// 点击日历空白格
			return;
		}
		if (this.data.calendarData.currentTab === 0) {
			// 日期选择
			// 大于当前日期前一天的禁止选择
			if (parseInt(item.isToday) > parseInt(getModifyTime(this.data.calendarData.today, -1))) return;
			this.mySetData({
				selectDate: item.isToday
			});
			app.globalData.choiceDateData.choiceData = item.isToday;
			app.globalData.choiceDateData.type = 1;
		} else {
			// 月份选择
			this.mySetData({
				selectMonth: item.currentDate
			});
			app.globalData.choiceDateData.choiceData = item.currentDate;
			app.globalData.choiceDateData.type = 2;
		}
		app.globalData.calendarData = this.data.calendarData;
		wx.navigateBack({
			delta: 1 // 默认值是1
		});
	},
	//  tab切换逻辑
	switchDatePickerTab (e) {
		let that = this;
		if (this.data.calendarData.currentTab === e.target.dataset.current) {
			return false;
		} else {
			that.mySetData({
				currentTab: e.target.dataset.current
			});
		}
	}
});
